package com.focusai.efairy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;


/**
 * 说明导航页
 */
public class FunctionWizardActivity extends SwipeBackBaseActivity {
    public final static String KEY_NO_START = "key_no_start";
    ViewPager myPager;
//    NaviIndicatorView indicatorView;
    private TextView mLoginBtn, mRegisterBtn, mOpenBtn;
    private final int[] images = {R.drawable.guide_img_guide_1,
            R.drawable.guide_img_guide_2, R.drawable.guide_img_guide_3};


    private boolean hideBtns = false;

    @Override
    protected boolean hasActionBar() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.act_function_wizard);

        findViews();
        initListener();
        initValue();
        initAnimationView();
    }

    private void initAnimationView() {
//        indicatorView = (NaviIndicatorView) findViewById(R.id.animation_indicator_view);
//        indicatorView.setCirclesCounts(images.length);
//        indicatorView.setCircleRadius(getResources().getDimensionPixelSize(R.dimen.dimen_4));
//        indicatorView.setCircleStoken(getResources().getDimensionPixelSize(R.dimen.dimen_10));
    }

    private void initValue() {
        mLoginBtn = (TextView) findViewById(R.id.login);
        mLoginBtn.setOnClickListener(new OnClickListener() { //登录
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FunctionWizardActivity.this, PasswdLoginActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                finish();
            }
        });
        mRegisterBtn = (TextView) findViewById(R.id.registrt);//注册
        mRegisterBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FunctionWizardActivity.this, PasswdLoginActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                finish();
            }
        });
        if (hideBtns) {
            mOpenBtn = (TextView) findViewById(R.id.openYzj);
            mOpenBtn.setVisibility(View.VISIBLE);
            mOpenBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            mRegisterBtn.setVisibility(View.GONE);
            mLoginBtn.setVisibility(View.GONE);
        }
        myPager.setAdapter(new WizardPagerAdapter());
        myPager.setOnPageChangeListener(new IndicatorPageChangeListener());
    }

    private void findViews() {
        myPager = (ViewPager) findViewById(R.id.vPager);
    }

    private void initListener() {
    }

    class IndicatorPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
//            if (indicatorView != null) {
//                indicatorView.onScroll(position, positionOffset);
//            }
        }

        @Override
        public void onPageSelected(int arg0) {
        }
    }

    class WizardPagerAdapter extends PagerAdapter {
        private LayoutInflater inflater;

        public WizardPagerAdapter() {
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        }


        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
            arg0.removeView((View) arg2);
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object instantiateItem(ViewGroup arg0, int arg1) {
            View layout = inflater.inflate(R.layout.wizard_image_item, null);
            ImageView image;
            image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(images[arg1]);
//            ImageLoaderFactory.getImageLoader().loadImage(FunctionWizardActivity.this,image, images[arg1], 0, false);
            if (arg1 == images.length - 1 && getIntent().getBooleanExtra(KEY_NO_START,true)){
                View btnStart = layout.findViewById(R.id.btn_start);
                btnStart.setVisibility(View.VISIBLE);
                btnStart.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FunctionWizardActivity.this, PasswdLoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
            }
//            imageTip = (ImageView) layout.findViewById(R.id.iv_guide_tips1);
//            tv2 = (TextView) layout.findViewById(R.id.tv_guide_tips2);
//            imageTip.setImageResource(tips[arg1]);
//            tv2.setText(tips2[arg1]);
            arg0.addView(layout);
            return layout;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

    }

}